package com.egao.common.module.cms.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.egao.common.component.elasticsearch.entity.ElasticSearchConstants;
import com.egao.common.component.elasticsearch.service.ElasticSearchService;
import com.egao.common.core.exception.ParameterException;
import com.egao.common.core.web.JsonResult;
import com.egao.common.core.web.PageParam;
import com.egao.common.core.web.PageResult;
import com.egao.common.module.cms.entity.Article;
import com.egao.common.module.cms.mapper.ArticleMapper;
import com.egao.common.module.cms.service.ArticleService;
import org.beetl.ext.fn.Json;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 文章管理实现类
 *
 * @author pyx
 */
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleService {
    @Autowired
    private ElasticSearchService elasticSearchService;

    @Override
    public List<Article> listPage(PageParam<Article> pageParam) {
        if (pageParam.getOrders().size() == 1 && "create_time".equals(pageParam.getOrders().get(0).getColumn())) {
            pageParam.getOrders().get(0).setColumn("ca.create_time");
        }

        return baseMapper.listPage(pageParam);
    }

    @Override
    public JsonResult insertArticle(Article article) {
        if (article == null) {
            throw new ParameterException("参数对象为空");
        }

        if (article.getCategoryId() == null) {
            throw new ParameterException("所属栏目不能为空");
        }
        if (StrUtil.isBlank(article.getTitle())) {
            throw new ParameterException("标题不能为空");
        }
        if (article.getTopFlag().equals("1") && topFlagNum(article.getCategoryId()).size() > 5) {
            throw new ParameterException("一个栏目下最多只能置顶6个文章");
        }

        baseMapper.insert(article);
        elasticSearchService.save(article);
        return JsonResult.ok("添加成功");
    }

    @Override
    public JsonResult updateArticle(Article article) {
        if (article == null) {
            throw new ParameterException("参数对象为空");
        }

        if (article.getCategoryId() == null) {
            throw new ParameterException("所属栏目不能为空");
        }
        if (StrUtil.isBlank(article.getTitle())) {
            throw new ParameterException("标题不能为空");
        }

        //处理修改文章是否是置顶文章
        if (article.getTopFlag().equals("1")) {
            List<Article> listTop = topFlagNum(article.getCategoryId());
            boolean num = true;
            for (Article a : listTop) {
                if (a.getId().equals(article.getId())) {
                    num = false;
                }
            }
            if (num && listTop.size() > 5) {
                throw new ParameterException("一个栏目下最多只能置顶6个文章");
            }
        }

        baseMapper.updateById(article);
        String id = ElasticSearchConstants.INFORMATION_ARTICLE + "-" + article.getId();
        elasticSearchService.delete(ElasticSearchConstants.INFORMATION_ARTICLE, id);
        elasticSearchService.save(article);
        return JsonResult.ok("修改成功");
    }

    @Override
    public JsonResult deleteArticle(Integer id) {
        if (id == null) {
            throw new ParameterException("参数对象为空");
        }

        if (removeById(id)) {
            return JsonResult.ok("删除成功");
        }
        String newId = ElasticSearchConstants.INFORMATION_ARTICLE + "-" + id;
        elasticSearchService.delete(ElasticSearchConstants.INFORMATION_ARTICLE, newId);
        return JsonResult.error("删除失败");
    }

    @Override
    public JsonResult getId(Integer id) {
        if (id == null) {
            throw new ParameterException("参数对象为空");
        }

        QueryWrapper<Article> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id);
        Integer count = baseMapper.selectCount(wrapper);

        if (count != 1) {
            throw new ParameterException("文章不存在");
        }

        Article article = baseMapper.selectById(id);
        return JsonResult.ok().setData(article);
    }

    @Override
    public List<Article> oldArticleList() {
        QueryWrapper<Article> articleQueryWrapper = new QueryWrapper<>();
        articleQueryWrapper.isNotNull("old_id");
        return baseMapper.selectList(articleQueryWrapper);
    }

    @Override
    public List<Article> listAll() {
        return baseMapper.selectList(null);
    }

    @Override
    public List<Article> topFlagNum(Integer categoryId) {
        if (categoryId == null) {
            throw new ParameterException("没有获取到栏目id");
        }

        QueryWrapper<Article> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category_id", categoryId);
        queryWrapper.eq("top_flag", 1);

        return baseMapper.selectList(queryWrapper);
    }
}
